﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ShoppingCart
{
    public class Cart:List<Item>
    {
        /// <summary>
        /// Add an Item to Cart
        /// </summary>
        /// <param name="Item"></param>
        public void AddToCart(Item item)
        {
            foreach (Item inCartItem in this)
            {
                //if exist
                
                if (inCartItem.ID == item.ID)
                {
                    inCartItem.Quantity += item.Quantity;
                    inCartItem.Total += item.Total;
                    return;
                }
            }

            Add(item);
        }

        /// <summary>
        /// Remove an Item from Cart
        /// </summary>
        /// <param name="item"></param>
        public void RemoveItem(Item item)
        {
            for (int i = 0; i < Count;i++ )
            {
                this.RemoveAt(i);
                return;
            }
        }

        /// <summary>
        /// Clear shopping cart
        /// </summary>
        public void ClearCart()
        {
            RemoveRange(0, Count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void UpdateItem(Item item)
        {
            foreach (Item inCartItem in this)
            {
                if(inCartItem.ID==item.ID){
                    inCartItem.Cost = item.Cost;
                    inCartItem.Name = item.Name;
                    inCartItem.Image = item.Image;
                    inCartItem.Quantity = item.Quantity;
                    inCartItem.Total = item.Total;
                    return;
                }
            }
        }
       
    }
}
